package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.PickCollectGoodsActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;

import android.support.v4.util.ArrayMap;

import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.GetCollectGoodsListRes;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.request.GetCollectGoodsListReq;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 领料收货列表  modle
 * <pre>
 * 1.所有的搜索都是跳转 <linked {@link com.supply.dl.activity.SearchActivity},跳转方式如下
 * Bundle bundle = new Bundle();
 * bundle.putString("modleName", PickBillSureWareHouseModle.class.getName());
 * jumpActivity(SearchActivity.class,bundle);
 *
 * 2.新建一个搜索页，是实现接口 ISearchModle 跳转去SearchActivity ，达到，不应在新建新的activty或者activity去匹配判断的目的
 *
 * 3.实现接口<linked {@link ISearchModle} 需要注意
 *  a.setSearchAdapter方法注定了 列表的样式数据，和点击事件，全部在这里进行自定义
 *  b.vistData方法 内部应该是进行访问获取数据，最后传递的参数，是对加载失败布局显示与否的说明。
 *
 * </pre>
 * <p>
 * Author: tian <br>
 * Time: 2018/4/28 15:09
 */
public class PickUpGoodsModle extends ISearchModle<GetCollectGoodsListRes> {

    @Override
    public void setSearchAdapter() {
        if (adapter != null)
            return;

        resource = new ArrayList<>();
        adapter = new NormalAdapter<GetCollectGoodsListRes>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, GetCollectGoodsListRes data) {

                holder.setText(R.id.name_tv, "领料单号:" + data.getNoticeOrderNo());
                holder.setText(R.id.status_tv, data.getOrderStatus());
                holder.setText(R.id.order_nub_tv, "生产订单号:" + data.getOrderNo());
                holder.setText(R.id.erp_nub_tv, "生产车间:" + data.getWorkshopName());

                holder.setText(R.id.time_tv, StringUtils.isEmpty(String.valueOf(data.getAddTime())) ? ""
                        : DateUtils.getSimpleDateTimeByMill(String.valueOf(data.getAddTime()), DateUtils.DATE_TIME_FORMAT));//建单时间

            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_obtainmaterial;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<GetCollectGoodsListRes>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, GetCollectGoodsListRes data, int position) {
                BaseActivity ac = (BaseActivity) context;
                Bundle bundle = new Bundle();
                bundle.putString("id", data.getId());
                ac.jumpActivity(PickCollectGoodsActivity.class, bundle);
            }
        });

    }

    @Override
    public void setHintText() {
        hintText = "领料单号，生产订单号";
    }

    @Override
    public void setTitleShow() {
        titleShow = "领料收货";
    }

    @Override
    public void init(Context context) {
        this.context = context;

        setTitleShow();
        setSearchAdapter();
    }


    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {
        GetCollectGoodsListReq req = new GetCollectGoodsListReq();
        req.key = keyWord;

        if (filterMap.get(AppConfig.SearchFilterAction.COMPANY) != null)
            req.companyResourceIds = filterMap.get(AppConfig.SearchFilterAction.COMPANY).split(",");
        if (filterMap.get(AppConfig.SearchFilterAction.FACTORY) != null)
            req.factoryResourceIds = filterMap.get(AppConfig.SearchFilterAction.FACTORY).split(",");
        if (filterMap.get(AppConfig.SearchFilterAction.WORKSHOP) != null)
            req.workshopResourceIds = filterMap.get(AppConfig.SearchFilterAction.WORKSHOP).split(".");

        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getCollectGoodsList(req, String.valueOf(pageNum), HttpManger.PAGE_SIZE)
                .compose(RxSchedulers.<BaseInfo<List<GetCollectGoodsListRes>>>compose(context))
                .subscribe(new BaseObserver<List<GetCollectGoodsListRes>>(context) {
                    @Override
                    public void onSuccess(BaseInfo<List<GetCollectGoodsListRes>> baseInfo) {
                        if (baseInfo != null) {
                            if (baseInfo.getData() != null) {
                                if(pageNum == 1){
                                    resource.clear();
                                }
                                if(baseInfo.getData().size() < 10){
                                    hasMoreData = false;
                                }
                                resource.addAll(baseInfo.getData());
                                adapter.notifyDataSetChanged();
                            }
                        }
                        if(resultListener != null)
                            resultListener.onResult();
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                        if(resultListener != null)
                            resultListener.onResult();
                    }
                });

        Toast.makeText(context, "vistData方法被调用", Toast.LENGTH_SHORT).show();
    }


}
